package com.yunlan.friendscenter.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunlan.friendscenter.common.ErrorCode;
import com.yunlan.friendscenter.exception.BusinessException;
import com.yunlan.friendscenter.pojo.Userdata;
import com.yunlan.friendscenter.pojo.request.UserLoginRequest;
import com.yunlan.friendscenter.pojo.request.UserRegisterRequest;
import com.yunlan.friendscenter.service.UserdataService;
import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Author: Wang
 * Date: 2022/4/29 14:25
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/run")
    public String run(){
        System.out.println("请求成功");
        return "请求成功";
    }

    @Autowired
    private UserdataService userdataService;

    @PostMapping("/register")
    public Long userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        if (userRegisterRequest==null){
           throw new BusinessException(ErrorCode.NULL_ERROR) ;
        }
       String userAccount = userRegisterRequest.getUserAccount();
       String userPassword = userRegisterRequest.getUserPassword();
       String checkPassword = userRegisterRequest.getCheckPassword();

       if (StringUtils.isAllBlank(userAccount, userPassword, checkPassword)){
          throw new BusinessException(ErrorCode.NULL_ERROR) ;
       }

        return userdataService.userRegister(userAccount, userPassword, checkPassword);
    }

    @PostMapping("/login")
    public Userdata userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
       if (userLoginRequest == null){
           throw new BusinessException(ErrorCode.NULL_ERROR,"请求数据为空");
       }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();

        if (StringUtils.isAllBlank(userAccount, userPassword)){
            return null;
        }

        return userdataService.userLogin(userAccount, userPassword,request);
    }

    @PostMapping("/loginOut")
    public Integer userLoginOut(HttpServletRequest request){
        if (request==null){
           throw new BusinessException(ErrorCode.NULL_ERROR,"请求体错误");
        }
        return userdataService.userLoginOut(request);
    }
    @GetMapping("/search")
    public Page<Userdata> searchUserData(@RequestParam(value = "username") String username, HttpServletRequest request, @RequestParam(required = false) String pagenum,
                                         @RequestParam(required = false) String pagesize,@RequestParam(required = false)String value){
        return userdataService.searchUserData(username,request,pagenum,pagesize,value);
    }

    @GetMapping("/delect")
    public boolean delectUserData(@RequestParam(value = "userAccount")String uid,HttpServletRequest request){
       return userdataService.delectUserData(Integer.valueOf(uid),request);
    }

}
